package com.ecy.quadratic.ui.fragment;

import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ListView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.CommunityListViewAdapter;
import com.ecy.quadratic.beans.Community;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.component.DaggerCommunityMainComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.contract.LastCommunityContract;
import com.ecy.quadratic.ui.presenter.LastCommunityPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;
import com.ecy.quadratic.widget.SwipeRefreshView.OnLoadListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class HotCommunityFragment extends BaseFragment implements LastCommunityContract.View {
    @Bind(R.id.community_hot_swipe_refreshview)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.community_hot_listview)
    ListView mComicListView;

    @Inject
    LastCommunityPresenter mPresenter;

    private CommunityListViewAdapter mAdapter;
    private List<Community> mCommunityList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;
    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            mPresenter.refresh("1");
        }
    };
    private OnLoadListener mOnLoadListener = new OnLoadListener() {

        @Override
        public void onLoad() {
            mPresenter.loadMore(mCurrentPage + 1 + "");
        }
    };

    public HotCommunityFragment() {
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_community_hot;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerCommunityMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
//		EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
//		EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        mCommunityList = new ArrayList<Community>();
        mAdapter = new CommunityListViewAdapter(this, mCommunityList);
    }

    @Override
    public void configViews() {
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.setOnLoadListener(mOnLoadListener);
        mComicListView.setAdapter(mAdapter);
        mSwipeRefreshView.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
    }

    @Override
    public void showRefreshList(PageInfo<Community> pageInfo) {

        if (pageInfo != null && mTotalPage != pageInfo.pages) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mCommunityList.addAll(pageInfo.list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showLoadMoreList(PageInfo<Community> pageInfo) {
        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mCommunityList.addAll(pageInfo.list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void postLikeSuccessful() {

    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false, true);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false);
    }
}
